<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='文章管理',active='article'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">文章管理</h4>
                    </div>
                    <div class="col-md-12">
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th width="35%">文章标题</th>
                                <th width="15%">发布时间</th>
                                <th>浏览量</th>
                                <th>所属分类</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <th:block th:each="article : ${articles.list}">
                                <tr th:id="${article.id}">
                                    <td>
                                        <a th:href="@{'/article/'+${article.id}}" th:text="${article.title}" target="_blank"></a>
                                    </td>
                                    <td><th:block th:text="${commons.dateFormat(article.created)}"/></td>
                                    <td><th:block th:text="${article.hits}"/></td>
                                    <td><th:block th:text="${article.categories}"/></td>
                                    <td>
                                        <a th:href="@{'/admin/article/'+${article.id}}"
                                           class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                                           <i class="fa fa-edit"></i> <span>编辑</span></a>
                                        <a href="javascript:void(0)" th:onclick="'delArticle('+${article.id}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                                            <i class="fa fa-trash-o"></i> <span>删除</span></a>
                                        <a class="btn btn-warning btn-sm waves-effect waves-light m-b-5" href="javascript:void(0)">
                                            <i class="fa fa-rocket"></i> <span>预览</span></a>
                                    </td>
                                </tr>
                            </th:block>

                            </tbody>
                        </table>
                        <div th:replace="comm/paging :: pageAdminNav(${articles})"></div>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>
<script type="text/javascript">
    function delArticle(id) {
        // 获取<meta>标签中封装的_csrf信息
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        if(confirm('确定删除该文章吗?')){
            $.ajax({
                type:'post',
                url : '/admin/article/delete',
                data: {id:id},
                dataType: 'json',
                beforeSend : function(xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result && result.success) {
                        window.alert("文章删除成功");
                        window.location.reload();
                    } else {
                        window.alert(result.msg || '文章删除失败')
                    }
                }
            });
        }
    }
</script>
</body>
</html>